package conson.tech.manage.facade.controller.scene;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import conson.tech.core.constant.SecurityConstants;
import conson.tech.core.exception.BaseException;
import conson.tech.core.response.Response;
import conson.tech.manage.common.util.ExcelUtils;
import conson.tech.manage.scene.feign.TrainingService;
import conson.tech.manage.scene.vo.TrainingAuditCourseVO;
import conson.tech.manage.scene.vo.TrainingAuditEvaluateVO;
import conson.tech.manage.scene.vo.request.TrainingCourseAuditRequest;
import conson.tech.manage.scene.vo.request.TrainingEvaluateAuditRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Description: 教培评价审核
 * @author: yuwenshuo
 * @since: 2020-06-22 09:58
 */

@Slf4j
@Api(value = "教培评价审核",tags = "教培评价审核")
@RestController
@RequestMapping((SecurityConstants.MANAGERAPI + "/training/evaluate"))
public class TrainingEvaluateAuditController extends BaseController{

    @Autowired
    private TrainingService trainingService;

    @ApiOperation(value = "教培评价审核列表", notes = "教培评价审核列表")
    @ApiImplicitParam(name = "trainingEvaluateAuditRequest", value = "请求入参", required = true, dataType = "TrainingCourseAuditRequest")
    @PostMapping("/list")
    public Response list(@RequestBody TrainingEvaluateAuditRequest trainingEvaluateAuditRequest, HttpServletRequest request) {
        return trainingService.queryAuditEvaluateList(trainingEvaluateAuditRequest);
    }


    @ApiOperation(value = "教培评价单个审核", notes = "教培评价单个审核")
    @ApiImplicitParam(name = "trainingEvaluateAuditRequest", value = "请求入参", required = true, dataType = "TrainingCourseAuditRequest")
    @PostMapping("/audit/single")
    public Response singleAudit(@RequestBody TrainingEvaluateAuditRequest trainingEvaluateAuditRequest, HttpServletRequest request) {
        trainingEvaluateAuditRequest.setAuditUser(super.getCurrentUserName(request));
        return trainingService.evaluateSingleAudit(trainingEvaluateAuditRequest);
    }


    @ApiOperation(value = "教培评价批量审核", notes = "教培评价批量审核")
    @ApiImplicitParam(name = "trainingEvaluateAuditRequest", value = "请求入参", required = true, dataType = "TrainingCourseAuditRequest")
    @PostMapping("/audit/batch")
    public Response batchAudit(@RequestBody TrainingEvaluateAuditRequest trainingEvaluateAuditRequest, HttpServletRequest request) {
        trainingEvaluateAuditRequest.setAuditUser(super.getCurrentUserName(request));
        return trainingService.evaluateBatchAudit(trainingEvaluateAuditRequest);
    }


    @ApiOperation(value = "教培评价审核导出", notes = "教培评价审核导出")
    @ApiImplicitParam(name = "trainingEvaluateAuditRequest", value = "请求入参", required = true, dataType = "TrainingCourseAuditRequest")
    @PostMapping("/audit/export")
    public void export(@RequestBody TrainingEvaluateAuditRequest trainingEvaluateAuditRequest, HttpServletRequest request, HttpServletResponse response) {

        Response<Page<TrainingAuditEvaluateVO>> res = trainingService.queryAuditEvaluateList(trainingEvaluateAuditRequest);
        if (res.failed()) {
            throw new BaseException();
        }

        Page page = res.getResult();

        String fileName = "evaluate_audit";

        try {
            ExcelUtils.export(fileName, page.getRecords(), TrainingAuditEvaluateVO.class, response);
        } catch (Exception e) {
            log.error("教培评价审核导出失败，exception", e);
        }

    }

}
